package com.mlxg.config.mmapper;

import com.mlxg.master.domain.HzExhibitor;
import com.mlxg.master.domain.HzExhibitsReceive;
import com.mlxg.master.domain.HzInformation;
import com.mlxg.master.domain.HzProductSeries;
import com.mlxg.master.domain.HzReviewReceive;
import com.mlxg.master.domain.HzReviewTypeReceive;
import com.mlxg.selver.domain.ExUser;
import com.mlxg.selver.domain.ExUserAttach;
import com.mlxg.selver.domain.ExUserInformation;
import com.mlxg.selver.domain.HzExhibitsSlave;
import com.mlxg.selver.domain.HzProductSeriesSlave;
import com.mlxg.selver.domain.HzReviewSlave;
import com.mlxg.selver.domain.HzReviewTypeSlave;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;

/**
 * @author cy zhang
 * @version 1.0
 * @date 2025/4/10 10:52
 */
@Component
public class InitMappingEntityCofing {
    @Autowired
    private ModelMapper modelMapper;

    @PostConstruct // 确保只执行一次
    private void configureAllMappings() {
        configureExUserMapping();
        configureExUserAttachMapping();
        configureHzProductSeriesMapping();
        configureHzExhibitsReceiveMapping();
        configureExUserInformationMapping();
        configureHzReviewMapping();
        configureHzReviewTypeMapping();
    }

    /**
     * 展商信息映射
     */
    private void configureExUserMapping() {
        // 检查是否已存在映射
        if (modelMapper.getTypeMap(HzExhibitor.class, ExUser.class) == null) {
            modelMapper.createTypeMap(HzExhibitor.class, ExUser.class)
                    .addMapping(HzExhibitor::getRegistAccount, ExUser::setAccount)//注册账号
                    .addMapping(HzExhibitor::getUserName, ExUser::setUserName) //用户名
                    .addMapping(HzExhibitor::getSex, ExUser::setUserSex) //性别
                    .addMapping(HzExhibitor::getUserTel, ExUser::setUserPhone)//手机号
//                    .addMapping(HzExhibitor::getUserTel, ExUser::setUserDepartment)//部门
//                    .addMapping(HzExhibitor::getColumn12, ExUser::setIsRecommend)//是否推荐
                    .addMapping(HzExhibitor::getEmail, ExUser::setUserEmail);//邮箱

        }
    }

    /**
     * 展商公司信息映射
     */
    private void configureExUserAttachMapping() {
        if (modelMapper.getTypeMap(HzExhibitor.class, ExUserAttach.class) == null) {
            modelMapper.createTypeMap(HzExhibitor.class, ExUserAttach.class)
                    .addMapping(HzExhibitor::getCompanyNameC, ExUserAttach::setCompanyNameC)
                    .addMapping(HzExhibitor::getCompanyNameE, ExUserAttach::setCompanyNameE)
                    .addMapping(HzExhibitor::getEnteProperty, ExUserAttach::setEnterpriseNature) //企业性质
                    .addMapping(HzExhibitor::getUnitAbbreviation, ExUserAttach::setEnterpriseAbbreviation) //企业简称
                    .addMapping(HzExhibitor::getAddressC, ExUserAttach::setCompanyAddress) //公司地址
                    .addMapping(HzExhibitor::getPostCode, ExUserAttach::setZipCode)//邮编
                    .addMapping(HzExhibitor::getTelephone, ExUserAttach::setCompanyPhone)
                    .addMapping(HzExhibitor::getComUrl, ExUserAttach::setCompanyWebsite)//网址
                    .addMapping(HzExhibitor::getComCover, ExUserAttach::setEnterpriseLogo) //公司logo中文
                    .addMapping(HzExhibitor::getRemarks, ExUserAttach::setEnterpriseLogoEn) //公司logo英文
                    .addMapping(HzExhibitor::getColumn16, ExUserAttach::setCompanyWechatPublicNumberUrl) //微信二维码
                    .addMapping(HzExhibitor::getColumn17, ExUserAttach::setCompanyIntroduceCn) //公司介绍中文
                    .addMapping(HzExhibitor::getColumn18, ExUserAttach::setCompanyIntroduceEn) //公司介绍引文
                    .addMapping(HzExhibitor::getColumn23, ExUserAttach::setEnterprisePublicityVideoUrl) //公司宣传视频封面地址
                    .addMapping(HzExhibitor::getColumn25, ExUserAttach::setVideoUrl); // 公司宣传视频地址
        }
    }

    /**
     * 自定义产品维护
     */
    private void configureHzProductSeriesMapping() {
        if (modelMapper.getTypeMap(HzProductSeries.class, HzProductSeriesSlave.class) == null) {
            modelMapper.createTypeMap(HzProductSeries.class, HzProductSeriesSlave.class)
                    .addMapping(HzProductSeries::getName, HzProductSeriesSlave::setName)
                    .addMapping(HzProductSeries::getSort, HzProductSeriesSlave::setSort)
                    .addMappings(mapper -> mapper.skip(HzProductSeriesSlave::setId)); // 显式跳过id字段
        }
    }

    /**
     * 展商展品数据映射
     */
    private void configureHzExhibitsReceiveMapping() {
        if (modelMapper.getTypeMap(HzExhibitsReceive.class, HzExhibitsSlave.class) == null) {
            modelMapper.createTypeMap(HzExhibitsReceive.class, HzExhibitsSlave.class)
                    .addMapping(HzExhibitsReceive::getExhibitsName, HzExhibitsSlave::setProductC)//  - 展品名称·中文
                    .addMapping(HzExhibitsReceive::getColumn25, HzExhibitsSlave::setProductE)  // -   - 展品名称·英文
                    .addMapping(HzExhibitsReceive::getColumn23, HzExhibitsSlave::setProductModel)//  -   - 展品型号
//                    .addMapping(HzExhibitsReceive::getColumn19, HzExhibitsSlave::setProductTypeId) //展品类型
//                    .addMapping(HzExhibitsReceive::getApplyTime, HzExhibitsSlave::setApplyTime) //申请时间
                    .addMapping(HzExhibitsReceive::getColumn11, HzExhibitsSlave::setIsRecommend) //是否推荐
//                    .addMapping(HzExhibitsReceive::getColumn15, HzExhibitsSlave::setProductCustomizeType) //  - 展品自定义类型
//                    .addMapping(HzExhibitsReceive::getExhibitsImg, HzExhibitsSlave::setProductPictureUrl) //展品图片
//                    .addMapping(HzExhibitsReceive::getExhibitsAttach, HzExhibitsSlave::setProductInformationUrl) //展品资料
                    .addMapping(HzExhibitsReceive::getExhibitsIntro, HzExhibitsSlave::setProductIntroduceC)//展品介绍中文
                    .addMapping(HzExhibitsReceive::getExhibitsIntroE, HzExhibitsSlave::setProductIntroduceE)//展品·介绍英文
                    .addMapping(HzExhibitsReceive::getTechParam, HzExhibitsSlave::setProductParameterC) //展品参数中文
                    .addMapping(HzExhibitsReceive::getColumn24, HzExhibitsSlave::setProductParameterE) //展品参数英文
//                    .addMapping(HzExhibitsReceive::getColumn20, HzExhibitsSlave::setProductVideoImageUrl)//  - 展品宣传视频封面
//                    .addMapping(HzExhibitsReceive::getColumn21, HzExhibitsSlave::setProductVideoUrl)// 产品视频
                    .addMappings(mapper -> mapper.skip(HzExhibitsSlave::setId)); // 显式跳过id字段
        }
    }

    /**
     * 展商资料映射
     */
    private void configureExUserInformationMapping() {
        if (modelMapper.getTypeMap(HzInformation.class, ExUserInformation.class) == null) {
            modelMapper.createTypeMap(HzInformation.class, ExUserInformation.class)
                    .addMapping(HzInformation::getInformationName, ExUserInformation::setInformationName)
                    .addMapping(HzInformation::getSort, ExUserInformation::setSort)
                    .addMapping(HzInformation::getLanguage, ExUserInformation::setLanguage)
                    .addMapping(HzInformation::getSize, ExUserInformation::setSize)

                    .addMappings(mapper -> mapper.skip(ExUserInformation::setAuditFlag))
                    .addMappings(mapper -> mapper.skip(ExUserInformation::setDelFlag))
                    .addMappings(mapper -> mapper.skip(ExUserInformation::setUrl))
                    .addMappings(mapper -> mapper.skip(ExUserInformation::setId)); // 显式跳过id字段
        }
    }

    /**
     * 往届展会回顾映射
     */
    private void configureHzReviewMapping() {
        if (modelMapper.getTypeMap(HzReviewReceive.class, HzReviewSlave.class) == null) {
            modelMapper.createTypeMap(HzReviewReceive.class, HzReviewSlave.class)//
                    .addMapping(HzReviewReceive::getReviewTitle, HzReviewSlave::setReviewTitle)//回顾标题
                    .addMapping(HzReviewReceive::getReviewName, HzReviewSlave::setReviewName)//回顾名称
                    .addMapping(HzReviewReceive::getReviewYear, HzReviewSlave::setReviewYear)//回顾年份
                    .addMapping(HzReviewReceive::getReviewImg, HzReviewSlave::setReviewImg)//回顾图片
                    .addMapping(HzReviewReceive::getReviewCoverImg, HzReviewSlave::setReviewCoverImg)//回顾封面图
                    .addMapping(HzReviewReceive::getColumn11, HzReviewSlave::setExpoTheme)//展会主题
                    .addMapping(HzReviewReceive::getColumn12, HzReviewSlave::setSupportingActivities)//配套活动
                    .addMapping(HzReviewReceive::getColumn13, HzReviewSlave::setExpoWebsite)//展会网站
                    .addMapping(HzReviewReceive::getColumn14, HzReviewSlave::setLanguage)//预留字段14语言类型0:中文 1:英文
                    .addMapping(HzReviewReceive::getColumn15, HzReviewSlave::setExhibitionArea)//展览面积
                    .addMapping(HzReviewReceive::getColumn16, HzReviewSlave::setParticipatingCountries)//参展国家
                    .addMapping(HzReviewReceive::getColumn17, HzReviewSlave::setExhibitorNumber)//展商数量
                    .addMapping(HzReviewReceive::getColumn18, HzReviewSlave::setVisitorsNumber)//参观人数
                    .addMapping(HzReviewReceive::getColumn19, HzReviewSlave::setExhibitionAddress)// 网站地址

                    .addMappings(mapper -> mapper.skip(HzReviewSlave::setAuditFlag))//
                    .addMappings(mapper -> mapper.skip(HzReviewSlave::setDelFlag))//
                    .addMappings(mapper -> mapper.skip(HzReviewSlave::setId)); // 显式跳过id字段
        }
    }


    /**
     * 往届展会回顾详情映射
     */
    private void configureHzReviewTypeMapping() {
        if (modelMapper.getTypeMap(HzReviewTypeReceive.class, HzReviewTypeSlave.class) == null) {
            modelMapper.createTypeMap(HzReviewTypeReceive.class, HzReviewTypeSlave.class)//
                    .addMapping(HzReviewTypeReceive::getTypeTitle, HzReviewTypeSlave::setTypeTitle)//回顾类型标题
                    .addMapping(HzReviewTypeReceive::getTypeContent, HzReviewTypeSlave::setTypeContent)//回顾类型内容
                    .addMapping(HzReviewTypeReceive::getColumn14, HzReviewTypeSlave::setExpoName)//会资料-get
                    .addMapping(HzReviewTypeReceive::getColumn15, HzReviewTypeSlave::setExproAttachment)//展会资料-附件
                    .addMapping(HzReviewTypeReceive::getColumn16, HzReviewTypeSlave::setLanguage)//预留字段16语言类型

                    .addMappings(mapper -> mapper.skip(HzReviewTypeSlave::setAuditFlag))//
                    .addMappings(mapper -> mapper.skip(HzReviewTypeSlave::setDelFlag))//
                    .addMappings(mapper -> mapper.skip(HzReviewTypeSlave::setHzReviewId))//
                    .addMappings(mapper -> mapper.skip(HzReviewTypeSlave::setId)); // 显式跳过id字段
        }
    }

}
